package com.woniu.maintain.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.common.entity.*;
import com.woniu.common.utils.Constants;
import com.woniu.common.utils.ResultVO;
import com.woniu.maintain.Feign.JpBedDrService;
import com.woniu.maintain.Feign.JpDepartmentService;
import com.woniu.maintain.Feign.JpHospitalService;
import com.woniu.maintain.Feign.JpUserService;
import com.woniu.maintain.service.JpMalfunctionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 啊川
 * @version 1.0
 * @date 2021/9/16 21:59
 * @QQ 836782490
 * @content:
 */
@RestController
@RequestMapping("JpMalfunction")
public class JpMalfunctionController {

    @Resource
    private JpMalfunctionService jpMalfunctionService;
    @Resource
    private JpDepartmentService jpDepartmentService;
    @Resource
    private JpBedDrService jpBedDrService;
    @Resource
    private JpHospitalService hospitalService;
    @Resource
    private JpUserService jpUserService;

    //查询所有故障记录
    @GetMapping("findAll")
    public ResultVO findAll() {
        try {
            List<JpMalfunction> jpMalfunctionList = jpMalfunctionService.findAll();
            System.out.println(jpMalfunctionList);
            if (jpMalfunctionList .size()!=0) {
                return ResultVO.success("故障记录查询成功", jpMalfunctionList);
            } else {
                return ResultVO.fail("故障记录查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("故障记录查询异常");
        }
    }

    //根据id查询故障记录
    @GetMapping("findById/{id}")
    public ResultVO<JpMalfunction> findById(@PathVariable("id") Integer id) {
        try {
            JpMalfunction jpMalfunction = jpMalfunctionService.findById(id);
            if (jpMalfunction == null) {
                return ResultVO.fail("故障记录查询失败");
            } else {
                return ResultVO.success("故障记录查询成功", jpMalfunction);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("故障记录查询异常");
        }
    }



    //新增故障记录
    @PostMapping("add")
    public ResultVO<JpMalfunction> add(JpMalfunction jpMalfunction ){
        try {
            Boolean falg = jpMalfunctionService.add(jpMalfunction);
            if (falg == false) {
                return ResultVO.fail("故障增记录加失败");
            } else {
                return ResultVO.success("故障记录增加成功", jpMalfunction);
            }
        } catch (Exception e) {
            return ResultVO.fail("故障记录增加异常");
        }
    }
    @PostMapping("/malfunction")
    public ResultVO addMalFunction(@RequestBody JpMalfunction malfunction) {
        boolean save = jpMalfunctionService.save(malfunction);
        malfunction.setStatue(1);
        Long insertid = jpMalfunctionService.getLastInsertId();

        JpMaintain maintain = new JpMaintain();

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 10; i++) {
            int random = (int) (Math.random() * 10);
            stringBuffer.append(random);
        }
        JpHospitalDept dept = hospitalDeptService.getById(malfunction.getDepartID());
        maintain.setBgrID(dept == null ? null : dept.getAccendant());
        maintain.setMainid(stringBuffer.toString());
        maintain.setMainStatue("待修复");
        maintain.setMalfunctionID(insertid);
        maintain.setDescribes("这是一条维修描述");

        maintain.setPersonnelId(dept == null ? null : dept.getAccendant());
        maintain.setId(stringBuffer.toString());
        maintain.setMainstatue("待修复");
        maintain.setMalfunctionid(insertid);
        maintainService.save(maintain);

        if (save) {
            return ResultVO.success("新增故障信息成功");
        } else {
            return ResultVO.fail("新增故障信息失败");
        }
    }




    //查询所有医院数据
    @GetMapping("findAllhospital")
    public ResultVO findAllhospital() {
        try {
            ResultVO resultVO = hospitalService.findAllHospital();
            if (resultVO.getCode() == Constants.SUCCESS) {
                JpHospitalinfo jpHospitalinfo = new JpHospitalinfo();
                BeanUtil.copyProperties(resultVO.getData(), jpHospitalinfo, true);
                if (resultVO.getData() == null) {
                    return ResultVO.fail("没有查询到医院");
                } else {
                    return ResultVO.success("医院查询成功", jpHospitalinfo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("医院查询异常");
        }
        return ResultVO.fail("医院查询失败");
    }

    //查询所有科室
    @GetMapping("findAlldepartment")
    public ResultVO findAlldepartment() {
        try {
            ResultVO resultVO = jpDepartmentService.findAllDepartment();
            if (resultVO.getCode() == Constants.SUCCESS) {
                JpDepartment jpDepartment = new JpDepartment();
                BeanUtil.copyProperties(resultVO.getData(), jpDepartment, true);
                if (resultVO.getData() == null) {
                    return ResultVO.fail("没有查询到科室");
                } else {
                    return ResultVO.success("科室查询成功", jpDepartment);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("科室查询异常");
        }
        return ResultVO.fail("科室查询失败");
    }

    //查询所有床位
    @GetMapping("findAllJpBedDr")
    public ResultVO findAllJpBedDr(){
        try {
            ResultVO resultVO = jpBedDrService.findAllJpBedDr();
            if (resultVO.getCode() == Constants.SUCCESS) {
                JpBedDr jpBedDr = new JpBedDr();
                BeanUtil.copyProperties(resultVO.getData(), jpBedDr, true);
                if (resultVO.getData() == null) {
                    return ResultVO.fail("没有查询到床位");
                } else {
                    return ResultVO.success("床位查询成功", jpBedDr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("床位查询异常");
        }
        return ResultVO.fail("床位查询失败");
    }
    //查询所有用户
    @GetMapping("findAllJpUser")
    public ResultVO findAllJpUser(){
        try {
            ResultVO resultVO = jpUserService.findAllJpUser();
            if (resultVO.getCode() == Constants.SUCCESS) {
                JpUser jpUser = new JpUser();
                BeanUtil.copyProperties(resultVO.getData(), jpUser, true);
                if (resultVO.getData() == null) {
                    return ResultVO.fail("没有查询到床位");
                } else {
                    return ResultVO.success("床位查询成功", jpUser);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.fail("床位查询异常");
        }
        return ResultVO.fail("床位查询失败");
    }
}
